﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using MM.Commmon.UI;
using DevExpress.XtraGrid.Views.Grid;
using System.Diagnostics;

namespace MM.Common.Setup
{
    public partial class FrmSetupWithItem : FrmFunctionWithTool
    {
        protected DataTable dtItem;
        protected DataTable dtItemAll;
        protected DataTable dtList;
        protected GridView gvItem;
        protected GridView gvList;
        protected bool isEdit;
        protected bool isNew;

        public FrmSetupWithItem()
        {
            this.InitializeComponent();
            this.dtList = new DataTable();
            this.dtItem = new DataTable();
            this.dtItemAll = new DataTable();
        }

        protected virtual void afterInsert()
        {
        }

        protected virtual void beforeSaveRecord()
        {
        }

        protected virtual void changeMode(bool edit)
        {
            this.isEdit = edit;
            this.gvList.GridControl.Enabled = !edit;
            this.pgBase.Enabled = this.isEdit;
            this.nbiNew.Visible = !this.isEdit;
            this.nbiDelete.Visible = !this.isEdit;
            this.nbiEdit.Visible = !this.isEdit;
            this.nbiChoice.Visible = this.isEdit;
            this.nbiSave.Visible = this.isEdit;
            this.nbiCancel.Visible = this.isEdit;
        }

        protected void ChoiceItem()
        {
            if (this.isEdit && (this.gvList.FocusedRowHandle >= 0))
            {
                if (this.dtItemAll.Rows.Count == 0)
                {
                    this.getItemAll();
                }
                this.getChoiceItem();
            }
        }

        protected virtual void deleteRecord()
        {
        }

        private void Export_Progress(object sender, DevExpress.XtraGrid.Export.ProgressEventArgs e)
        {
            if (e.Phase == DevExpress.XtraGrid.Export.ExportPhase.Link)
            {
                this.progressBarControl1.Position = e.Position;
                base.Update();
            }
        }

        private void ExportTo(DevExpress.XtraExport.IExportProvider provider)
        {
            Cursor current = Cursor.Current;
            Cursor.Current = Cursors.WaitCursor;
            base.FindForm().Refresh();
            DevExpress.XtraGrid.Export.BaseExportLink link = this.gvList.CreateExportLink(provider);
            (link as DevExpress.XtraGrid.Export.GridViewExportLink).ExpandAll = false;
            link.Progress += new DevExpress.XtraGrid.Export.ProgressEventHandler(this.Export_Progress);
            link.ExportTo(true);
            provider.Dispose();
            link.Progress -= new DevExpress.XtraGrid.Export.ProgressEventHandler(this.Export_Progress);
            Cursor.Current = current;
        }

        protected virtual void freshDisp()
        {
        }

        protected virtual void getChoiceItem()
        {
        }

        protected virtual void getItemAll()
        {
        }

        protected void gvItem_DoubleClick(object sender, EventArgs e)
        {
            this.ChoiceItem();
        }

        private void gvList_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            if (e.FocusedRowHandle >= 0)
            {
                this.freshDisp();
            }
        }

        public override bool Init()
        {
            if (base.Init())
            {
                this.changeMode(false);
                this.gvList.FocusedRowChanged += new DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventHandler(this.gvList_FocusedRowChanged);
                this.gvItem.DoubleClick += new EventHandler(this.gvItem_DoubleClick);
                return true;
            }
            return false;
        }

        protected void InitData()
        {
            this.gvItem.GridControl.DataSource = this.dtItem;
            this.gvList.GridControl.DataSource = this.dtList;
        }

        private void nbiCancel_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.changeMode(false);
            this.freshDisp();
        }

        private void nbiChoice_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.ChoiceItem();
        }

        private void nbiDelete_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.gvList.GetFocusedDataRow() != null)
            {
                this.deleteRecord();
            }
        }

        private void nbiEdit_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            if (this.gvList.FocusedRowHandle >= 0)
            {
                this.changeMode(true);
                this.isNew = false;
            }
        }

        private void nbiExcel_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.ucPanel1.Visible = true;
            string fileName = this.ShowSaveFileDialog("Excel 文件", "Excel 文件|*.xls");
            if (fileName != "")
            {
                //this.ExportTo(new DevExpress.XtraExport.ExportXlsProvider(fileName));
                this.OpenFile(fileName);
            }
            this.ucPanel1.Visible = false;
        }

        private void nbiExit_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            base.Close();
        }

        private void nbiHTML_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string fileName = this.ShowSaveFileDialog("HTML 文件", "HTML 文件|*.html");
            if (fileName != "")
            {
                this.ExportTo(new DevExpress.XtraExport.ExportHtmlProvider(fileName));
                this.OpenFile(fileName);
            }
        }

        private void nbiNew_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.changeMode(true);
            this.isNew = true;
            this.afterInsert();
        }

        private void nbiPrint_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            Cursor.Current = Cursors.WaitCursor;
            //if (DevExpress.XtraPrinting.PrintHelper.IsPrintingAvailable)
            //{
            //    DevExpress.XtraPrinting.PrintHelper.ShowPreview(this.gvList.GridControl);
            //}
            //else
            //{
            //    DevExpress.XtraEditors.XtraMessageBox.Show("打印库文件未找到！", "提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
            //}
            //Cursor.Current = Cursors.Default;
        }

        private void nbiSave_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            this.beforeSaveRecord();
            if (this.saveRecord())
            {
                this.changeMode(false);
                this.freshDisp();
            }
        }

        private void nbiTXT_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
            string fileName = this.ShowSaveFileDialog("文本文件", "文本文件|*.txt");
            if (fileName != "")
            {
                this.ExportTo(new DevExpress.XtraExport.ExportTxtProvider(fileName));
                this.OpenFile(fileName);
            }
        }

        private void nbiXML_LinkClicked(object sender, DevExpress.XtraNavBar.NavBarLinkEventArgs e)
        {
        }

        private void OpenFile(string fileName)
        {
            if (DevExpress.XtraEditors.XtraMessageBox.Show("是否打开文件?", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                try
                {
                    new System.Diagnostics.Process { StartInfo = { FileName = fileName, Verb = "Open", WindowStyle = ProcessWindowStyle.Normal } }.Start();
                }
                catch
                {
                    DevExpress.XtraEditors.XtraMessageBox.Show(this, "不能找到打开该文件的系统。", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Hand);
                }
            }
            this.progressBarControl1.Position = 0;
        }

        protected virtual bool saveRecord()
        {
            return true;
        }

        private string ShowSaveFileDialog(string title, string filter)
        {
            SaveFileDialog dialog = new SaveFileDialog();
            string tableName = this.dtList.TableName;
            int startIndex = tableName.LastIndexOf(".") + 1;
            if (startIndex > 0)
            {
                tableName = tableName.Substring(startIndex, tableName.Length - startIndex);
            }
            dialog.Title = "导出到：" + title;
            dialog.FileName = tableName;
            dialog.Filter = filter;
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                return dialog.FileName;
            }
            return "";
        }

        private void ucPanel1_Resize(object sender, EventArgs e)
        {
        }
    }
}
